home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / NewGUI / IFF_Support / Src / iff_support.e
Text File  |  1998-07-04  |  4KB  |  173 lines

  1. /* 
  2.  *  Loading/Saveing Files using the iffparse.library
  3.  * -================================================-
  4.  * 
  5.  * 
  6.  */
  7.  
  8. OPT     OSVERSION = 37
  9. OPT     MODULE
  10.  
  11. MODULE  'dos/dos'
  12. MODULE  'iffparse'
  13. MODULE  'libraries/iffparse'
  14.  
  15. /*
  16. ENUM    ERR_NONE=0,
  17.         ERR_MEM=1
  18.  
  19. DEF     file=NIL,
  20.         mode=0
  21.  
  22. EXPORT PROC main()     HANDLE
  23.  DEF    buf[80]:STRING,
  24.         id=0,
  25.         size=0,
  26.         type=0,
  27.         finish=FALSE,
  28.         node=NIL,
  29.         error=FALSE,
  30.         got=0
  31.   IF (file:=iff_init())
  32.    mode:=iff_open(file,'RAM:Demo.IFF',NEWFILE,"DEMO")
  33.  
  34.     StrCopy(buf,'Demo-String1')
  35.      iff_write(file,"STR1",buf,StrLen(buf)+1)
  36.     StrCopy(buf,'String2 of the DEMO')
  37.      iff_write(file,"STR2",buf,StrLen(buf)+1)
  38.  
  39.    iff_close(file,mode)
  40.  
  41. ->
  42.    mode:=iff_open(file,'RAM:Demo.IFF',OLDFILE)
  43.  
  44.     WHILE finish=FALSE
  45.      finish,node:=iff_step(file)
  46.       IF (node<>NIL)
  47.        id,size,type:=iff_info(node)
  48.  
  49.         WriteF('ID = $\h Type = $\h, Size = \d\n',id,type,size)
  50.  
  51.          IF (id<>ID_FORM)
  52.           error,got:=iff_read(file,buf,size)
  53.  
  54.            WriteF('\d Bytes Readed - Data = "\s"\n',got,buf)
  55.          ENDIF
  56.       ENDIF
  57.     ENDWHILE
  58.  
  59.    iff_close(file,mode)
  60.   ELSE
  61.    Raise(ERR_MEM)
  62.   ENDIF
  63.  
  64. EXCEPT DO
  65. IF (file<>NIL) THEN iff_close(file,mode)
  66.  iff_exit(file)
  67.   IF exception
  68.    SELECT       exception
  69.         CASE    ERR_MEM
  70.                 WriteF('Out of memory!\n')
  71.    ENDSELECT
  72.   ENDIF
  73.  CleanUp(exception)
  74. ENDPROC
  75.  
  76. */
  77.  
  78. EXPORT PROC iff_init()
  79.  DEF    iffhandle=NIL:PTR TO iffhandle
  80.   IF (iffparsebase:=OpenLibrary('iffparse.library',37))
  81.    iffhandle:=AllocIFF()
  82.   ELSE
  83.    RETURN FALSE
  84.   ENDIF
  85. ENDPROC iffhandle
  86.  
  87. EXPORT PROC iff_exit(iffhandle:PTR TO iffhandle)
  88.   IF (iffhandle<>NIL) THEN FreeIFF(iffhandle)
  89.  IF (iffparsebase<>NIL) THEN CloseLibrary(iffparsebase)
  90. iffhandle:=NIL
  91. ENDPROC
  92.  
  93. EXPORT PROC iff_open(iffhandle:PTR TO iffhandle,filename,mode,id=0)
  94.  IF (iffhandle<>NIL)
  95.   iffhandle.stream:=Open(filename,mode)
  96.    IF (iffhandle.stream<>NIL)
  97.     InitIFFasDOS(iffhandle)
  98.      IF (mode=NEWFILE)
  99.       OpenIFF(iffhandle,IFFF_WRITE)
  100.        PushChunk(iffhandle,id,ID_FORM,IFFSIZE_UNKNOWN)
  101.      ELSE
  102.       OpenIFF(iffhandle,IFFF_READ)
  103.      ENDIF
  104.    ELSE
  105.     RETURN NIL
  106.    ENDIF
  107.  ENDIF
  108. ENDPROC mode
  109.  
  110. EXPORT PROC iff_close(iffhandle:PTR TO iffhandle,mode)
  111.  IF (iffhandle<>NIL)
  112.   IF (mode=NEWFILE)
  113.    PopChunk(iffhandle)
  114.   ENDIF
  115.     CloseIFF(iffhandle)
  116.    IF (iffhandle.stream<>NIL) THEN Close(iffhandle.stream)
  117.   iffhandle.stream:=NIL
  118.  ENDIF
  119. ENDPROC
  120.  
  121. EXPORT PROC iff_write(iffhandle,id,data,size)
  122.  IF (iffhandle<>NIL)
  123.   PushChunk(iffhandle,0,id,IFFSIZE_UNKNOWN)
  124.    WriteChunkBytes(iffhandle,data,size)
  125.   PopChunk(iffhandle)
  126.  ENDIF
  127. ENDPROC
  128.  
  129. EXPORT PROC iff_beginchunk(iffhandle,id)
  130.  IF (iffhandle<>NIL)
  131.   PushChunk(iffhandle,0,id,IFFSIZE_UNKNOWN)
  132.  ENDIF
  133. ENDPROC
  134.  
  135. EXPORT PROC iff_writeraw(iffhandle,data,size)
  136.  IF (iffhandle<>NIL)
  137.   WriteChunkBytes(iffhandle,data,size)
  138.  ENDIF 
  139. ENDPROC
  140.  
  141. EXPORT PROC iff_endchunk(iffhandle)
  142.  IF (iffhandle<>NIL)
  143.   PopChunk(iffhandle)
  144.  ENDIF
  145. ENDPROC
  146.  
  147. EXPORT PROC iff_read(iffhandle,buf,size)
  148.  DEF    error=FALSE
  149.   IF (iffhandle<>NIL) AND (size>0)
  150.    error:=ReadChunkBytes(iffhandle,buf,size)
  151.   ELSE
  152.    error:=TRUE
  153.   ENDIF
  154. ENDPROC error,size
  155.  
  156. EXPORT PROC iff_info(cn:PTR TO contextnode)    IS      cn.id, cn.size, cn.type
  157.  
  158. EXPORT PROC iff_step(iffhandle:PTR TO iffhandle)
  159.  DEF    error=0,
  160.         fin=FALSE,
  161.         node=NIL
  162.   IF (iffhandle<>NIL)
  163.    error:=ParseIFF(iffhandle,IFFPARSE_RAWSTEP)
  164.     IF (error=IFFERR_EOC)       -> -2
  165.      fin,node:=iff_step(iffhandle)
  166.     ELSEIF error
  167.      fin:=TRUE
  168.     ELSE
  169.      node:=CurrentChunk(iffhandle)
  170.     ENDIF
  171.   ENDIF
  172. ENDPROC fin, node
  173.